const path = require("path");
const { fileUploadError, unSupportFileType } = require("../constants/err.type");

class GoodsController {
  async upload(ctx, next) {
    try {
      // file 名称是跟入参键对应
      const { file } = ctx.request.files;
      console.log(file, "file");
      const fileTypes = ["image/jpeg", "image/png"];
      if (file) {
        if (!fileTypes.includes(file.mimetype)) {
          return ctx.app.emit("error", unSupportFileType, ctx);
        }
        ctx.body = {
          code: 0,
          message: "图片上传成功",
          result: {
            img: path.basename(file.filepath),
          },
        };
      } else {
        console.log(error);
        return ctx.app.emit("error", fileUploadError, ctx);
      }
    } catch (error) {
      console.log(error);
      return ctx.app.emit("error", fileUploadError, ctx);
    }
  }
}

module.exports = new GoodsController();
